package com.koron.housing.apply.service.impl;


import com.koron.housing.apply.bean.po.HousingStaffHonor;
import com.koron.housing.apply.mapper.HousingStaffHonorMapper;
import com.koron.housing.apply.service.HousingStaffHonorService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 工作荣誉(HousingStaffHonor)表服务实现类
 *
 * @author jzp
 * @since 2022-02-26 15:22:40
 */
@Service("honorService")
public class HousingStaffHonorServiceImpl implements HousingStaffHonorService {

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public HousingStaffHonor getById(SessionFactory factory, String id) {
        HousingStaffHonorMapper mapper = factory.getMapper(HousingStaffHonorMapper.class);
        return mapper.selectById(id);
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<HousingStaffHonor> list(SessionFactory factory, HousingStaffHonor housingStaffHonor) {
        HousingStaffHonorMapper mapper = factory.getMapper(HousingStaffHonorMapper.class);
        return mapper.selectList(housingStaffHonor);
    }

    @Override
    @TaskAnnotation("listByApplyId")
    public List<HousingStaffHonor> listByApplyId(SessionFactory factory, String applyId) {
        HousingStaffHonor housingStaffHonor = new HousingStaffHonor();
        housingStaffHonor.setApplyId(applyId);
        return list(factory, housingStaffHonor);
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, HousingStaffHonor housingStaffHonor) {
        HousingStaffHonorMapper mapper = factory.getMapper(HousingStaffHonorMapper.class);
        return mapper.insertOrUpdate(housingStaffHonor) > 0;
    }

    @Override
    @TaskAnnotation("saveBatch")
    public boolean saveBatch(SessionFactory factory, List<HousingStaffHonor> honorList) {
        if (CollectionUtils.isEmpty(honorList)) {
            return false;
        }
        HousingStaffHonorMapper mapper = factory.getMapper(HousingStaffHonorMapper.class);
        return mapper.insertOrUpdateBatch(honorList) > 0;
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, HousingStaffHonor housingStaffHonor) {
        HousingStaffHonorMapper mapper = factory.getMapper(HousingStaffHonorMapper.class);
        return mapper.update(housingStaffHonor) > 0;
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        HousingStaffHonorMapper mapper = factory.getMapper(HousingStaffHonorMapper.class);
        return mapper.hidden(id) > 0;
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteBatchIds")
    public boolean deleteBatchIds(SessionFactory factory, List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        HousingStaffHonorMapper mapper = factory.getMapper(HousingStaffHonorMapper.class);
        return mapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @TaskAnnotation("removeByApplyId")
    public boolean removeByApplyId(SessionFactory factory, String applyId) {
        if (StringUtils.isEmpty(applyId)) {
            return false;
        }
        List<HousingStaffHonor> honorList = listByApplyId(factory, applyId);
        return deleteBatchIds(factory, honorList.stream().map(HousingStaffHonor::getId).collect(Collectors.toList()));

    }
}
